﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace _404park.Utilities.Extensions
{
    public static class StringExtensions
    {
        public static string CleanUpDescription(string bareDescription)
        {
            string description = bareDescription.Replace("<![CDATA[", string.Empty).Replace("]]>", string.Empty);
            return StripTags(HtmlDecode(description)).Trim();
        }

        public static string StripTags(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        public static string HtmlDecode(this string html)
        {
            return HttpUtility.HtmlDecode(html).Trim();
        }

        public static string HtmlEncode(this string html)
        {
            return HttpUtility.HtmlEncode(html).Trim();
        }

        public static string RemoveSpaces(this string searchTerm)
        {
            return Regex.Replace(searchTerm, @"\s+", " ");
        }
    }
}
